<template>
	<view>
		<cu-custom :bgColor="'bg-'+ theme.themeColor" :isBack="false">
			<block slot="content">商品分类</block>
		</cu-custom>
		<view class="VerticalBox">
			<scroll-view class="VerticalNav nav" scroll-y scroll-with-animation :scroll-top="verticalNavTop"
				style="height:100vh">
				<view class="cu-item" :class="index==tabCur?'cur text-'+theme.themeColor:''"
					v-for="(item,index) in list" :key="index" @tap="tabSelect" :data-id="index">
					{{item.name}}
				</view>
			</scroll-view>
			<scroll-view class="VerticalMain" scroll-y scroll-with-animation style="height:100vh"
				:scroll-into-view="'main-'+mainCur" @scroll="verticalMain">
				<view class="padding-top-xs padding-lr-xs" v-for="(item,index) in list" :key="index"
					:id="'main-'+index">
					<view class="cu-bar solid-bottom bg-white">
						<view class="action">
							<text class="cuIcon-title text-green" :class="'text-'+ theme.matchColor"></text>
							{{item.name}}
						</view>
					</view>
					<view class="cu-bar bg-white">
						<view class="flex flex-wrap">
							<view v-for="(childrenItem,childrenIndex)  in item.children" :key="childrenIndex"
								@tap="doGoods(childrenItem.id)" class="text-xs text-center padding-xs">
								<image style="width: 140rpx;height: 140rpx;"
									:src="childrenItem.categoryPic ? childrenItem.categoryPic : ''">
								</image>
								<view class="text-sm">{{childrenItem.name}}</view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>
<script>
	import {
		getList
	} from '@/api/goodscategory.js';
	export default {
		data() {
			return {
				theme: getApp().globalData.theme,
				list: [],
				tabCur: 0,
				mainCur: 0,
				verticalNavTop: 0,
				load: true
			};
		},
		onLoad() {
			this.getGoodsCategory();
		},
		onReady() {
			uni.hideLoading()
		},
		methods: {
			/**
			 * 获取商品分类
			 * @param {Object} 
			 */
			getGoodsCategory() {
				getList().then(res => {
					this.list = res.data
				});
			},
			/**
			 * 跳转商品列表
			 */
			doGoods(id) {
				uni.navigateTo({
					url: '/pages/goods/goods-list/index?categorySecondId=' + id
				})
			},
			tabSelect(e) {
				this.tabCur = e.currentTarget.dataset.id;
				this.mainCur = e.currentTarget.dataset.id;
				this.verticalNavTop = (e.currentTarget.dataset.id - 1) * 50
			},
			verticalMain(e) {
				// #ifdef MP-ALIPAY
				return false //支付宝小程序暂时不支持双向联动 
				// #endif
				let that = this;
				let tabHeight = 0;
				if (this.load) {
					for (let i = 0; i < this.list.length; i++) {
						let view = uni.createSelectorQuery().select("#main-" + i);
						view.fields({
							size: true
						}, data => {
							this.list[i].top = tabHeight;
							tabHeight = tabHeight + data.height;
							this.list[i].bottom = tabHeight;
						}).exec();
					}
					this.load = false
				}
				let scrollTop = e.detail.scrollTop + 10;
				for (let i = 0; i < this.list.length; i++) {
					if (scrollTop > this.list[i].top && scrollTop < this.list[i].bottom) {
						this.verticalNavTop = (i - 1) * 50
						this.tabCur = i
						console.log(scrollTop)
						return false
					}
				}
			}
		},
	}
</script>

<style>
	.fixed {
		position: fixed;
		z-index: 99;
	}

	.VerticalNav.nav {
		width: 200upx;
		white-space: initial;
	}

	.VerticalNav.nav .cu-item {
		width: 100%;
		text-align: center;
		background-color: #fff;
		margin: 0;
		border: none;
		height: 50px;
		position: relative;
	}

	.VerticalNav.nav .cu-item.cur {
		background-color: #f1f1f1;
	}

	.VerticalNav.nav .cu-item.cur::after {
		content: "";
		width: 8upx;
		height: 30upx;
		border-radius: 10upx 0 0 10upx;
		position: absolute;
		background-color: currentColor;
		top: 0;
		right: 0upx;
		bottom: 0;
		margin: auto;
	}

	.VerticalBox {
		display: flex;
	}

	.VerticalMain {
		background-color: #f1f1f1;
		flex: 1;
	}
</style>
